package com.apes.scm.masterdata.product.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.party.model.Party;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRepository;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.product.model.Classify;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author LJP
 * 2021/04/09
 */
@Service("PurchaseSellRangeService")
public class PurchaseSellRangeService extends DomainService {

    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private PartyRoleRepository partyRoleRepository;
    @Autowired
    private PartyRepository partyRepository;

    /**
     * 判断采购经销范围
     * @param simpleRequest
     * 参数:productId(必需) companyId(可有可无)
     * 返回:当数据存在策略执行表，返回true，否则 返回false
     */
    public boolean checkPurchaseSellRange(SimpleRequest simpleRequest){
        JSONObject json = simpleRequest.getJO();
        String productId = json.getString("productId");
        if (productId == null) throw new RuntimeException("必须存在商品ID");

        JSONObject jsonObject = new JSONObject();

        if (json.getString("companyId") != null){
            PartyRole customer = partyRoleRepository.findOne(json.getString("customerId"));
            PartyRole customerParty = partyRoleRepository.simpleFind(customer.getParty().getId(), "Company");
            Party party = partyRepository.findOne(customerParty.getParty().getId());
            if (party != null && party.getCorporate() != null) {
                jsonObject.put("OPERATION", party.getCorporate().getOperation().getId());
            }

        }

        jsonObject.put("conditionStrategyId","PD_TCJXFW");

        Product product = productRepository.findOne(productId);
        if (product == null) throw  new RuntimeException("数据库不存在该商品");
        Classify classify = product.getClassify();
        jsonObject.put("PRODUCT_ID",product.getId());
        jsonObject.put("CLASSIFY_ID", classify != null ? classify.getId() : null);
        jsonObject.put("BRAND_ID",product.getBrand() != null ? product.getBrand().getId() : null);
        jsonObject.put("SYS_RQ", new Date());

        Object obj = null;
        try {
            obj = this.invoke("condition.execConditionStrategyNoSession",jsonObject);
        } catch (Exception e) {
            System.out.println("--------");
            if (e.getMessage().indexOf("判断统采代转经范围规则[TCJXFW]无符合条件的返回数据！") != -1) {
                return false;
            } else {
                throw new RuntimeException(e.getMessage());
            }
        }
        String str = obj.toString();
        if (str.contains("true")) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 判断经转代范围
     * @param simpleRequest
     * 参数:productId(必需) companyId(必需)
     * 返回:当数据存在策略执行表，返回true，否则 返回false
     */
    public boolean checkReplacementRange(SimpleRequest simpleRequest){
        JSONObject json = simpleRequest.getJO();
        String productId = json.getString("productId");
        if (productId == null) throw new RuntimeException("必须存在商品ID");
        if (json.getString("companyId") == null) return false;
        JSONObject jsonObject = new JSONObject();

        if (json.getString("companyId") != null){
            PartyRole customer = partyRoleRepository.findOne(json.getString("customerId"));
            PartyRole customerParty = partyRoleRepository.simpleFind(customer.getParty().getId(), "Company");
            Party party = partyRepository.findOne(customerParty.getParty().getId());
            if (party != null && party.getCorporate() != null) {
                jsonObject.put("OPERATION", party.getCorporate().getOperation().getId());
            }
        }

        jsonObject.put("conditionStrategyId","PD_TCJZDFW");

        Product product = productRepository.findOne(productId);
        if (product == null) throw  new RuntimeException("数据库不存在该商品");
        Classify classify = product.getClassify();
        jsonObject.put("PRODUCT_ID",product.getId());
        jsonObject.put("CLASSIFY_ID",classify.getId());
        jsonObject.put("BRAND_ID", product.getBrand() != null ? product.getBrand().getId() : null);
        jsonObject.put("SYS_RQ", new Date());

        Object obj = null;
        try {
            obj = this.invoke("condition.execConditionStrategyNoSession",jsonObject);
        } catch (Exception e) {
            if (e.getMessage().indexOf("判断统采经转代范围规则[TCJZDFW]无符合条件的返回数据") != -1) {
                return false;
            } else {
                throw new RuntimeException(e.getMessage());
            }
        }
        String str = obj.toString();
        if (str.contains("true")) {
            return true;
        }else {
            return false;
        }
    }


}
